package com.ticket.web.controller.yanchu;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ticket.common.exception.ServiceException;
import com.ticket.system.domain.YanchuRefundOrder;
import com.ticket.system.domain.vo.RefundBackendOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.enums.BusinessType;
import com.ticket.system.domain.YanchuRefundBackendOrder;
import com.ticket.system.service.IYanchuRefundBackendOrderService;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.common.core.page.TableDataInfo;

/**
 * 出票退款单Controller
 * 
 * @author ruoyi
 * @date 2024-02-19
 */
@Api(description = "出票退票管理")
@RestController
@RequestMapping("/system/backendOrder")
public class YanchuRefundBackendOrderController extends BaseController
{
    @Autowired
    private IYanchuRefundBackendOrderService yanchuRefundBackendOrderService;

    /**
     * 查询出票退款单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(YanchuRefundBackendOrder yanchuRefundBackendOrder)
    {
        startPage();
        List<YanchuRefundBackendOrder> list = yanchuRefundBackendOrderService.selectYanchuRefundBackendOrderList(yanchuRefundBackendOrder);
        return getDataTable(list);
    }

    /**
     * 导出出票退款单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "出票退款单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YanchuRefundBackendOrder yanchuRefundBackendOrder)
    {
        List<YanchuRefundBackendOrder> list = yanchuRefundBackendOrderService.selectYanchuRefundBackendOrderList(yanchuRefundBackendOrder);
        ExcelUtil<YanchuRefundBackendOrder> util = new ExcelUtil<YanchuRefundBackendOrder>(YanchuRefundBackendOrder.class);
        util.exportExcel(response, list, "出票退款单数据");
    }

    /**
     * 获取出票退款单详细信息
     */
    @ApiOperation("获取出票退款单详细信息")
    @GetMapping(value = "/{id}")
    @ApiImplicitParam(name = "id", value = "编码", dataType = "Long", dataTypeClass = Long.class)
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yanchuRefundBackendOrderService.selectYanchuRefundBackendOrderById(id));
    }

    /**
     * 获取出票退款单详细信息
     */
    @ApiOperation("获取出票退款单详细信息")
    @GetMapping(value = "/detail/{idDetail}")
    @ApiImplicitParam(name = "idDetail", value = "订单详情编码", dataType = "Long", dataTypeClass = Long.class)
    public AjaxResult<YanchuRefundBackendOrder> getBackendInfo(@PathVariable("idDetail") Long idDetail)
    {
        return success(yanchuRefundBackendOrderService.selectRefundBackendOrderById(idDetail));
    }
    /**
     * 新增退款单
     */
    @ApiOperation("新增出票退款单")
    @ApiImplicitParam(name = "yanchuRefundOrder", value = "退款单对象", dataType = "YanchuRefundOrder", dataTypeClass = YanchuRefundOrder.class)
    @PreAuthorize("@ss.hasPermi('system:backendOrderRefund:add')")
    @Log(title = "退款单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid YanchuRefundBackendOrder yanchuRefundBackendOrder)
    {
        try {
            return toAjax(yanchuRefundBackendOrderService.insertYanchuRefundOrder(yanchuRefundBackendOrder,null));
        }catch(ServiceException se){
            logger.error("新增出票退款单ServiceException",se);
            return AjaxResult.error(se.getMessage());
        }
        catch (Exception e){
            logger.error("新增出票退款单失败",e);
            return AjaxResult.error("新增退款单失败");
        }
    }

    /**
     * 修改出票退款单付款
     */
    @PreAuthorize("@ss.hasPermi('system:backendOrder:pay')")
    @Log(title = "出票退款单", businessType = BusinessType.UPDATE)
    @ApiOperation("修改出票退款单付款")
    @ApiImplicitParam(name = "yanchuRefundOrder", value = "退款单对象", dataType = "RefundBackendOrderVo", dataTypeClass = RefundBackendOrderVo.class)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid RefundBackendOrderVo refundBackendOrderVo)
    {
        YanchuRefundBackendOrder backendOrder = new YanchuRefundBackendOrder();
        backendOrder.setId(refundBackendOrderVo.getId());
        backendOrder.setRefundStatus(refundBackendOrderVo.getRefundStatus());
        backendOrder.setRefundDemo(refundBackendOrderVo.getRefundDemo());
        backendOrder.setRefundWay(refundBackendOrderVo.getRefundWay());
        backendOrder.setRefundReason(refundBackendOrderVo.getRefundReason());
        backendOrder.setRefundPayName(refundBackendOrderVo.getRefundPayName());
        return toAjax(yanchuRefundBackendOrderService.updateYanchuRefundBackendOrder(backendOrder));
    }

    /**
     * 删除出票退款单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "出票退款单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yanchuRefundBackendOrderService.deleteYanchuRefundBackendOrderByIds(ids));
    }
}
